import {defineStore} from 'pinia';
import {AppState} from '@/store/modules/app/types';
import {sysOssTokenInfo} from '@/api/api-sys.ts';

const useAppStore = defineStore('app', {
  state: (): AppState => {
    return {
      ossToken: undefined,
    };
  },
  getters: {},
  actions: {
    updatePatch(state: Partial<AppState>) {
      this.$patch(state);
    },
    // 获取OSS令牌
    async getOssToken(fileName: string) {
      let ossToken = this.ossToken

      if (!ossToken || (ossToken?.expire * 1000) < new Date().getTime()) {
        const { body } = await sysOssTokenInfo({action: 'sts', type: 2, file: fileName})
        this.ossToken = body
      }
      return this.ossToken
    },
    // 重置OSS令牌信息¬
    async resetOssToken() {
      this.ossToken = undefined
    }
  },
});

export default useAppStore;
